package cn.article.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.article.dto.ConditionDto;
import cn.article.entity.ArticleInfo;
import cn.article.entity.Province;
import cn.article.repository.ArticleGrep;
import cn.article.repository.ProvinceGrep;
import cn.article.service.ArticleInterface;
import cn.easier.club.base.dto.ResultInfo;
import net.sf.json.JSONArray;

@Service
public class ArticleServiceImpl implements ArticleInterface {

	private static Logger log = LoggerFactory.getLogger(ArticleInterface.class);

	@Resource
	private ArticleGrep articleGrep;
	@Resource
	private ProvinceGrep provinceGrep;

	@Override
	public ResultInfo saveArticle(ArticleInfo articleInfo) {
		ResultInfo resultInfo = null;
		try {
			resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS",
					articleGrep.saveAndFlush(articleInfo));
		} catch (Exception e) {
			log.error("save articleInfo fail=>{}", e);
			e.printStackTrace();
		}
		return resultInfo;
	}

	@Override
	public ArticleInfo findByArticleNum(String articleNum) {
		ArticleInfo result = null;
		try {
			result = articleGrep.findByArticleNum(articleNum);
		} catch (Exception e) {
			log.error("articleNum,query articleInfo fail=>{}", e);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<ArticleInfo> findAllByConditions(ConditionDto cons) {
		List<ArticleInfo> results = null;
		try {
			if (cons.getCenterStore()== null && cons.getProvinceStore()== null && cons.getCreateTime()== null
					&& cons.getArticleNum() == null && cons.getArticleName() == null) {// 查询所有
				results = articleGrep.findAll();
				log.info("查询所有",results);
			} else if (cons.getCenterStore() != null && cons.getProvinceStore() != null && cons.getCreateTime() != null
					&& cons.getArticleNum() == null && cons.getArticleName() == null) {// 中心仓，省仓，时间查询
                results = articleGrep.findAllByCenterStoreAndProvinceStoreAndCreateTime(cons.getCenterStore(), cons.getProvinceStore(), formatDate(cons.getCreateTime()));
                log.info("中心仓，省仓，时间查询",results);
			}else if(cons.getCenterStore() != null && cons.getProvinceStore() != null && cons.getCreateTime() != null
					&& cons.getArticleNum() == null && cons.getArticleName()!= null){//中心仓，省仓，时间及产品名称进行查询
			    results = articleGrep.findAllByCenterStoreAndProvinceStoreAndCreateTimeAndArticleName(cons.getCenterStore(), cons.getProvinceStore(), formatDate(cons.getCreateTime()), cons.getArticleName());
			    log.info("中心仓，省仓，时间及产品名称进行查询",results);
			}else if(cons.getCenterStore() != null && cons.getProvinceStore()== null && cons.getCreateTime() != null
					&& cons.getArticleNum() == null && cons.getArticleName()== null){//中心仓、时间查询
			  results = articleGrep.findAllByCenterStoreAndCreateTime(cons.getCenterStore(), formatDate(cons.getCreateTime()));
			  log.info("中心仓、时间查询",results);
			}else if(cons.getCenterStore()== null && cons.getProvinceStore()!= null && cons.getCreateTime() != null
					&& cons.getArticleNum() == null && cons.getArticleName()== null){//省仓、时间查询 
				 results = articleGrep.findAllByProvinceStoreAndCreateTime(cons.getProvinceStore(), formatDate(cons.getCreateTime()));
				 log.info("省仓、时间查询",results);
			}
		} catch (Exception e) {
			log.error("query articleInfo fail=>{}", e);
			e.printStackTrace();
		}
		return results;
	}

	
	
	public Date formatDate(String createTime) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sf.parse(createTime);
		} catch (ParseException e) {
			log.info("时间格式转换异常=>{}", e);
			e.printStackTrace();
		}
		return date;
	}

	@Override
	public ResultInfo saveProvince(Province province) {
		ResultInfo resultInfo = null;
		try {
			resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS",
					provinceGrep.saveAndFlush(province));
		} catch (Exception e) {
			log.error("save province fail=>{}", e);
			e.printStackTrace();
		}
		return resultInfo;
	}

	@Override
	public ResultInfo findAll() {
		ResultInfo result = null;
		JSONArray jsonArray = null;
		List<Province> resultInfo=null;
		try {
			resultInfo= provinceGrep.findAll();
			jsonArray = JSONArray.fromObject(resultInfo);
			result=new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS",
					jsonArray.toString());
		} catch (Exception e) {
			log.error("query province fail=>{}", e);
			e.printStackTrace();
		}
		return result;
	}


}
